package com.oceansoft.mobile.biz.api.controller;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.oceansoft.mobile.common.Constant;
import com.oceansoft.mobile.common.util.CacheBaseService;
import com.oceansoft.mobile.common.util.HttpClientUtil;
import com.oceansoft.mobile.common.util.Result;
import com.oceansoft.mobile.weixin.util.WeixinUtil;

@Controller
@RequestMapping("api/notice")
public class ApiNoticeController {
	@RequestMapping(value = "{pageName}", method = RequestMethod.GET)
	public String gotoPages(@PathVariable("pageName") String pageName,HttpServletRequest request, Model model) {
		String source = (String) request.getSession().getAttribute("source");
	       if (StringUtils.isNotBlank(source)) {
	       	model.addAttribute("source", source);
	       }
		return "weixin/notice/" + pageName;
	}

	@RequestMapping(value = "{type}/{id}", method = RequestMethod.GET)
	public String gotoPages(@PathVariable("type") int type,
			@PathVariable("id") int id,HttpServletRequest request, Model model) {
		String pageName = "";
		model.addAttribute("id", id);
		switch (type) {
		case 1:
			pageName = "bafwgsxq";
			break;
		case 2:
			pageName = "bapxgsxq";
			break;
		case 3:
			pageName = "jssggsxq";
			break;
		case 4:
			pageName = "ksglxq";
			break;
		}
		String source = (String) request.getSession().getAttribute("source");
	       if (StringUtils.isNotBlank(source)) {
	       	model.addAttribute("source", source);
	       }
		return "weixin/notice/" + pageName;
	}

	@ResponseBody
	@RequestMapping("unlock/allArea")
	public Result getAllArea() throws IOException {
		String url = Constant.host+"/api/unlockCompany/allArea";
        Result r = null;
        if(CacheBaseService.getInstance().exists(url))
        {
        	 r = CacheBaseService.getInstance().getObject(url, Result.class);
        }
        else
        { 
        	r = HttpClientUtil.doGAGet(url);
        	CacheBaseService.getInstance().cache(url, r, 1f);
        }
		return r;
	}
	
	@ResponseBody
	@RequestMapping(value = "unlock/page", method = POST)
	public Result loadUnlockCompanyByPage(
			@RequestParam(value = "name", defaultValue = "") final String name,
			@RequestParam(value = "pageIndex") final int pageIndex,
			@RequestParam(value = "area", defaultValue = "") final String area,
			@RequestParam("pageSize") final int pageSize)  throws IOException {
		//WeixinUtil.gesearchCountInfo("15","其他");//统计方法调用（暂时用不到）
		String url = Constant.host+"/api/unlockCompany/page?name="+name+"&pageIndex="+pageIndex+"&area="+area+"&pageSize="+pageSize;
        Result r = null;
        JSONObject param = new JSONObject();
//    	param.put("name",name);
//    	param.put("pageIndex",pageIndex);
//    	param.put("area",area);
//    	param.put("pageSize",pageSize);
    	r = HttpClientUtil.doGAUrlPost(url, param);
		return r;
	}
	
	
	
	@ResponseBody
	@RequestMapping("scrapMetal/allArea")
	public Result getAllArea1() throws IOException {
		String url = Constant.host+"/api/scrapMetal/allArea";
        Result r = null;
        if(CacheBaseService.getInstance().exists(url))
        {
        	 r = CacheBaseService.getInstance().getObject(url, Result.class);
        }
        else
        { 
        	r = HttpClientUtil.doGAGet(url);
        	CacheBaseService.getInstance().cache(url, r, 1f);
        }
		return r;
	}
	
	@ResponseBody
	@RequestMapping(value = "scrapMetal/page", method = POST)
	public Result loadScrapMetalByPage(
			@RequestParam(value = "name", defaultValue = "") final String name,
			@RequestParam(value = "pageIndex") final int pageIndex,
			@RequestParam(value = "area", defaultValue = "") final String area,
			@RequestParam("pageSize") final int pageSize)  throws IOException {
		//WeixinUtil.gesearchCountInfo("15","其他");//查詢统计方法调用（暂时用不到）
		String url = Constant.host+"/api/scrapMetal/page?name="+name+"&pageIndex="+pageIndex+"&area="+area+"&pageSize="+pageSize;
        Result r = null;
        JSONObject param = new JSONObject();
//    	param.put("name",name);
//    	param.put("pageIndex",pageIndex);
//    	param.put("area",area);
//    	param.put("pageSize",pageSize);
    	r = HttpClientUtil.doGAUrlPost(url, param);
		return r;
	}
	
	
	
	@ResponseBody
	@RequestMapping("securityTrain/allArea")
	public Result getAllArea2() throws IOException {
		String url = Constant.host+"/api/securityTrain/allArea";
        Result r = null;
        if(CacheBaseService.getInstance().exists(url))
        {
        	 r = CacheBaseService.getInstance().getObject(url, Result.class);
        }
        else
        { 
        	r = HttpClientUtil.doGAGet(url);
        	CacheBaseService.getInstance().cache(url, r, 1f);
        }
		return r;
	}
	
	@ResponseBody
	@RequestMapping(value = "securityTrain/page", method = POST)
	public Result loadSecurityTrainByPage(
			@RequestParam(value = "name", defaultValue = "") final String name,
			@RequestParam(value = "pageIndex") final int pageIndex,
			@RequestParam(value = "area", defaultValue = "") final String area,
			@RequestParam("pageSize") final int pageSize)  throws IOException {
		//WeixinUtil.gesearchCountInfo("15","其他");//查詢统计方法调用（暂时用不到）
		String url = Constant.host+"/api/securityTrain/page?name="+name+"&pageIndex="+pageIndex+"&area="+area+"&pageSize="+pageSize;
        Result r = null;
        JSONObject param = new JSONObject();
//    	param.put("name",name);
//    	param.put("pageIndex",pageIndex);
//    	param.put("area",area);
//    	param.put("pageSize",pageSize);
    	r = HttpClientUtil.doGAUrlPost(url, param);
		return r;
	}
	
	
	
	@ResponseBody
	@RequestMapping("securityService/allArea")
	public Result getAllArea3() throws IOException {
		String url = Constant.host+"/api/securityService/allArea";
        Result r = null;
        if(CacheBaseService.getInstance().exists(url))
        {
        	 r = CacheBaseService.getInstance().getObject(url, Result.class);
        }
        else
        { 
        	r = HttpClientUtil.doGAGet(url);
        	CacheBaseService.getInstance().cache(url, r, 1f);
        }
		return r;
	}
	
	@ResponseBody
	@RequestMapping(value = "securityService/page", method = POST)
	public Result loadSecurityServiceByPage(
			@RequestParam(value = "name", defaultValue = "") final String name,
			@RequestParam(value = "pageIndex") final int pageIndex,
			@RequestParam(value = "area", defaultValue = "") final String area,
			@RequestParam("pageSize") final int pageSize)  throws IOException {
		//WeixinUtil.gesearchCountInfo("15","其他");//查詢统计方法调用（暂时用不到）
		String url = Constant.host+"/api/securityService/page?name="+name+"&pageIndex="+pageIndex+"&area="+area+"&pageSize="+pageSize;
        Result r = null;
        JSONObject param = new JSONObject();
//    	param.put("name",name);
//    	param.put("pageIndex",pageIndex);
//    	param.put("area",area);
//    	param.put("pageSize",pageSize);
    	r = HttpClientUtil.doGAUrlPost(url, param);
		return r;
	}
	
	@ResponseBody
	@RequestMapping("securityService/view/{id}")
	public Result securityServiceDetail(@PathVariable("id")String id) throws IOException {
		String url = Constant.host+"/api/securityService/view/"+id;
        Result r = null;
        if(CacheBaseService.getInstance().exists(url))
        {
        	 r = CacheBaseService.getInstance().getObject(url, Result.class);
        }
        else
        { 
        	String result = HttpClientUtil.doGet(url);
        	JSONObject obj = JSONObject.parseObject(result);
        	r=new Result();
        	r.setData(obj);
        	CacheBaseService.getInstance().cache(url, r, 1f);
        }
		return r;
	}
	
	@ResponseBody
	@RequestMapping("securityTrain/view/{id}")
	public Result securityTrainDetail(@PathVariable("id")String id) throws IOException {
		String url = Constant.host+"/api/securityTrain/view/"+id;
        Result r = null;
        if(CacheBaseService.getInstance().exists(url))
        {
        	 r = CacheBaseService.getInstance().getObject(url, Result.class);
        }
        else
        { 
        	String result = HttpClientUtil.doGet(url);
        	JSONObject obj = JSONObject.parseObject(result);
        	r=new Result();
        	r.setData(obj);
        	CacheBaseService.getInstance().cache(url, r, 1f);
        }
		return r;
	}
	@ResponseBody
	@RequestMapping("scrapMetal/view/{id}")
	public Result scrapMetalDetail(@PathVariable("id")String id) throws IOException {
		String url = Constant.host+"/api/scrapMetal/view/"+id;
        Result r = null;
        if(CacheBaseService.getInstance().exists(url))
        {
        	 r = CacheBaseService.getInstance().getObject(url, Result.class);
        }
        else
        { 
        	String result = HttpClientUtil.doGet(url);
        	JSONObject obj = JSONObject.parseObject(result);
        	r=new Result();
        	r.setData(obj);
        	CacheBaseService.getInstance().cache(url, r, 1f);
        }
		return r;
	}
	@ResponseBody
	@RequestMapping("unlockCompany/view/{id}")
	public Result unlockCompanyDetail(@PathVariable("id")String id) throws IOException {
		String url = Constant.host+"/api/unlockCompany/view/"+id;
        Result r = null;
        if(CacheBaseService.getInstance().exists(url))
        {
        	 r = CacheBaseService.getInstance().getObject(url, Result.class);
        }
        else
        { 
        	String result = HttpClientUtil.doGet(url);
        	JSONObject obj = JSONObject.parseObject(result);
        	r=new Result();
        	r.setData(obj);
        	CacheBaseService.getInstance().cache(url, r, 1f);
        }
		return r;
	}
}
